package com.ad.pet.message;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.MainActivity;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.account.order.OrderStatusActivity;
import com.ad.pet.common.data.utils.ui.DialogUtil;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.constant.Constant;
import com.ad.pet.message.adapter.SystemMsgAdapter;
import com.ad.pet.models.remote.message.Message;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by SEELE on 2018/8/16.
 */

public class SystemMessageActivity extends AppToolbarActivity implements MessageContract.View {
    @Inject
    MessagePresenter mMessagePresenter;
    private String mCid;
    @BindView(R.id.recycler_message)
    RecyclerView mRecyclerView;
    private SystemMsgAdapter mSystemMsgAdapter;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);

        mMessagePresenter.getTypeMessage(mCid);
        mToolbar.setTitle(getString(R.string.system_message));
//        mToolbar.setTitleIconClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                List<Message> messages = mSystemMsgAdapter.getData();
//                if (messages == null || messages.size() == 0) {
//                    ToastUtil.getToastUtil().showShort(getString(R.string.not_need_clear_read_msg));
//                    return;
//                }
//                int number = 0;
//                for (int i = 0; i < messages.size(); i++) {
//                    if (messages.get(i).getRead() == 1) {
//                        number++;
//                    }
//                }
//                if (messages.size() == number) {
//                    ToastUtil.getToastUtil().showShort(getString(R.string.not_need_clear_read_msg));
//                    return;
//                }
//                DialogUtil.createDialog(mActivity, getString(R.string.tips), getString(R.string.is_clear_all_message), null, getString(R.string.confirm), new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        mMessagePresenter.isTypeReadMessage(mCid);
//                    }
//                });
//
//            }
//        });
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_system_msg;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mMessagePresenter.onCreate(this);
        mCid = getIntent().getStringExtra(Constant.C_ID);

        mMessagePresenter.getTypeMessage(mCid);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mSystemMsgAdapter = new SystemMsgAdapter(null);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mSystemMsgAdapter.bindToRecyclerView(mRecyclerView);
        mRecyclerView.setAdapter(mSystemMsgAdapter);




    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        mSystemMsgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Message> messages = mSystemMsgAdapter.getData();
                if (messages != null) {
                    Message message = messages.get(position);
                    mMessagePresenter.isReadMessage(message.getId());

//                        Navigation.goCommonWebView(mActivity, message.getTitle(), message.getLocation_url(), false, MainActivity.REQUEST_GO_MALL);
                    String linkUrl = message.getLocation_url();
                    if (linkUrl.contains("http")) {
                        Navigation.goCommonWebView(mActivity, "", linkUrl);
                    }else if ("AccountBalanceChange".equals(linkUrl)){

                        Navigation.goRecordList(mActivity);

                    }else if ("RedEnvelopeChange".equals(linkUrl)){


                    }else if ("TheCouponHasArrived".equals(linkUrl)){


                    }else if("CouponsAreAboutToExpire".equals(linkUrl)){


                    }else if ("CartPriceReduced".equals(linkUrl)){

                        Intent intent = new Intent();
                        intent.putExtra("goHome",2);
                        setResult(RESULT_OK,intent);
                        finish();

                    }else if ("CartUnpaid".equals(linkUrl)){

                        Intent intent = new Intent();
                        intent.putExtra("goHome",2);
                        setResult(RESULT_OK,intent);
                        finish();

                    }else if ("OrderUnpaid".equals(linkUrl)){

                        Intent intent = new Intent(mActivity, OrderStatusActivity.class);
                        intent.putExtra(Constant.STATUS,"unpaid");
                        intent.putExtra(Constant.NAME,mActivity.getString(R.string.unpaid));
                        mActivity.startActivity(intent);
                    }else if ("HasSendGood".equals(linkUrl)){
                        Intent intent = new Intent(mActivity, OrderStatusActivity.class);
                        intent.putExtra(Constant.STATUS,"paid");
                        intent.putExtra(Constant.NAME,mActivity.getString(R.string.paid));
                        mActivity.startActivity(intent);

                    }else if("WishlistPriceReduced".equals(linkUrl)){
                        Navigation.goCollectin(mActivity,MainActivity.REQUEST_GO_MALL);
                    }
                }
            }
        });

        mSystemMsgAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final BaseQuickAdapter adapter, View view, final int position) {
                DialogUtil.createDialog(mActivity, getString(R.string.tips), getString(R.string.is_delete_message), null, getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mMessagePresenter.deleteMessage(mSystemMsgAdapter.getData().get(position).getId());
                    }
                });
                return true;
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {
        mSystemMsgAdapter.setEmptyView(R.layout.layout_empty);
    }

    @Override
    public void error(int code, String msg) {
        mSystemMsgAdapter.setEmptyView(R.layout.layout_empty);
    }

    @Override
    public void getMessageSuccess(List<Message> messages) {

        RxBus.getDefault().post("set_message_number_success");
    }

    @Override
    public void getTypeMessageSuccess(List<Message> messages) {
        mStatusLayoutManager.showContent();
        int num = 0;
        for (int i = 0; i < messages.size(); i++) {
            if (messages.get(i).getRead() == 0) {
                num++;
            }
        }
        mToolbar.setTitle(getString(R.string.system_message));
        mSystemMsgAdapter.setNewData(messages);
        if (num>0){
            mToolbar.getTitleIcon().setVisibility(View.VISIBLE);
            mToolbar.setIconTitle(num);
        }else {
            mToolbar.getTitleIcon().setVisibility(View.GONE);

        }

        if (messages==null||messages.size()==0){
            mSystemMsgAdapter.setEmptyView(R.layout.layout_empty);
        }


    }

    @Override
    public void deleteMessageSuccess() {
        mMessagePresenter.getTypeMessage(mCid);
        RxBus.getDefault().post("set_message_number_success");
    }

    @Override
    public void isReadMessageSuccess() {
        mMessagePresenter.getTypeMessage(mCid);
        RxBus.getDefault().post("set_message_number_success");
    }

    @Override
    public void isTypeReadMessageSuccess() {
        mMessagePresenter.getTypeMessage(mCid);
        RxBus.getDefault().post("set_message_number_success");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        switch (requestCode) {

            case MainActivity.REQUEST_GO_MALL:
                setResult(RESULT_OK, data);
                finish();

                break;

        }
    }
}
